package com.ruoyi.web.controller.datastatistics;

import com.ruoyi.basedata.domain.TerminalsDb;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.datastatistics.domain.TerminalHistoryData;
import com.ruoyi.datastatistics.service.ITerminalHistoryDataService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.util.List;

/**
 * 大-历史数据Controller
 *
 * @author ruoyi
 * @date 2023-09-06
 */
@RestController
@RequestMapping("/datastatistics/terminalhistory")
public class TerminalHistoryDataController extends BaseController {
    @Autowired
    private ITerminalHistoryDataService terminalHistoryDataService;

    /**
     * 查询大-历史数据列表
     */
    @PreAuthorize("@ss.hasPermi('datastatistics:terminalhistory:list')")
    @GetMapping("/listAll")
    public AjaxResult listAll(TerminalsDb terminalsDb) throws ParseException {
        String startTime = terminalsDb.getParams().get("startTime").toString();
        String endTime = terminalsDb.getParams().get("endTime").toString();
        List<TerminalHistoryData> list = terminalHistoryDataService.getTerminalHistoryDataList(terminalsDb.getTerminalId(), startTime, endTime,null,null);
        return success(list);
    }

    /**
     * 查询大-历史数据列表
     */
    @PreAuthorize("@ss.hasPermi('datastatistics:terminalhistory:list')")
    @GetMapping("/list")
    public TableDataInfo list(TerminalsDb terminalsDb) throws ParseException {
        startPage();
        String startTime = terminalsDb.getParams().get("startTime").toString();
        String endTime = terminalsDb.getParams().get("endTime").toString();
        List<TerminalHistoryData> list = terminalHistoryDataService.getTerminalHistoryDataList(terminalsDb.getLocationNb(), startTime, endTime,null,null);
        return getDataTable(list);
    }

    /**
     * 导出大-历史数据列表
     */
    @PreAuthorize("@ss.hasAnyPermi('datastatistics:terminalhistory:export,datastatistics:terminalhistory:list')")
    @Log(title = "大-历史数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TerminalsDb terminalsDb) throws ParseException {
        startOrderBy();
        // 只显示压力数据标识
        String onlyShowPressure = "-Y";
        // 只显示流量数据标识
        String onlyShowFlow = "-L";
        ExcelUtil<TerminalHistoryData> util = new ExcelUtil<>(TerminalHistoryData.class);
        String startTime = terminalsDb.getParams().get("startTime").toString();
        String endTime = terminalsDb.getParams().get("endTime").toString();
        List<TerminalHistoryData> list = terminalHistoryDataService.getTerminalHistoryDataList(terminalsDb.getLocationNb(), startTime, endTime, null, null);
        if (StringUtils.endsWithIgnoreCase(terminalsDb.getTypeId(), onlyShowPressure)) {
            util.hideColumn("totalFlow", "readNumber", "reverseNumber", "instantNumber", "instantVelocity");
        } else if (StringUtils.endsWithIgnoreCase(terminalsDb.getTypeId(), onlyShowFlow)) {
            util.hideColumn("pressure");
        }
        util.exportExcel(response, list, "大-历史数据数据");
    }

    /**
     * 获取大-历史数据详细信息
     */
    @PreAuthorize("@ss.hasPermi('datastatistics:terminalhistory:query')")
    @GetMapping(value = "/{TerminalID}")
    public AjaxResult getInfo(@PathVariable("TerminalID") String TerminalID) {
        return success(terminalHistoryDataService.selectTerminalHistoryDataByTerminalID(TerminalID));
    }

    /**
     * 新增大-历史数据
     */
    @PreAuthorize("@ss.hasPermi('datastatistics:terminalhistory:add')")
    @Log(title = "大-历史数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TerminalHistoryData terminalHistoryData) {
        return toAjax(terminalHistoryDataService.insertTerminalHistoryData(terminalHistoryData));
    }

    /**
     * 修改大-历史数据
     */
    @PreAuthorize("@ss.hasPermi('datastatistics:terminalhistory:edit')")
    @Log(title = "大-历史数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TerminalHistoryData terminalHistoryData) {
        return toAjax(terminalHistoryDataService.updateTerminalHistoryData(terminalHistoryData));
    }

    /**
     * 删除大-历史数据
     */
    @PreAuthorize("@ss.hasPermi('datastatistics:terminalhistory:remove')")
    @Log(title = "大-历史数据", businessType = BusinessType.DELETE)
    @DeleteMapping("/{TerminalIDs}")
    public AjaxResult remove(@PathVariable String[] TerminalIDs) {
        return toAjax(terminalHistoryDataService.deleteTerminalHistoryDataByTerminalIDs(TerminalIDs));
    }
}
